package cn.jdy.platform.tag;

import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.SpringContextUtils;
import cn.jdy.platform.entity.ConfValue;
import cn.jdy.platform.service.ConfService;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * 获取配置信息
 *
 * @description :
 * @author: zhaiguangtao
 * @data: 2010-8-9
 */
public class ConfTag extends TagSupport {
	private static final long serialVersionUID = 1L;
	private String id;// 对应配置项目 typeid
	private String type;// str 单一字符串输出，ads广告类型
	private String style;// 广告类型时的样式

	@Override
	public int doStartTag() throws JspException {// 这个方法不用所以直接返回值
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {// 重点在这个方法上
		try {
			ConfService conf = SpringContextUtils.getBean(ConfService.class);
			ConfValue cf = conf.find(Long.valueOf(id));
			JspWriter out = pageContext.getOut();
			if("ads".equals(type)) {// 广告输出
				if(null == cf || null == cf.getValue2() || "".equals(cf.getValue2())) {//
					out.print("");// 如果未配置图片 则不显示
				} else {
					out.print("<a target='_blank' href='" + cf.getValue1() + "'> <IMG src='" + cf.getValue2() + "' style='" + style + "'/></a>");
				}
			} else if("value0".equals(type)) {
				out.print(cf.getValue0() == null ? "" : cf.getValue0());// 配置值-文字
			} else if("value1".equals(type)) {
				out.print(cf.getValue1() == null ? "" : cf.getValue1());// 配置值-链接
			} else if("value2".equals(type)) {
				out.print(cf.getValue2() == null ? "" : cf.getValue2());// 配置值-图片地址
			} else if("value3".equals(type)) {
				out.print(cf.getValue3() == null ? "" : cf.getValue3());// 配置值-扩展
			} else if("value4".equals(type)) {
				out.print(cf.getValue4() == null ? "" : cf.getValue4());// 配置值-扩展
			} else {
				out.print(cf.getValue0() == null ? "" : cf.getValue0());
			}
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "获取配置项异常："+id, e);
		}
		return EVAL_PAGE;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

}
